<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;
use App\Models\SystemUserRole;
class RoleController extends Controller
{
    use ResponseData;
    public function list(SystemUserRole $model,Request $request)
    {
        $model =  $model
            ->with('department','first','two','three')
            ->select('id','zh_name','name','created_at','department_one_id','department_two_id','department_three_id','department_id');
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        $role = $model->get();
        return $this->success($role);
    }

    public function store(RoleRequest $request)
    {
        $data = $request->all();
        $data['guard_name'] = 'sanctum';
        $role = Role::create($data);
        return $this->success($role);
    }

    public function edit(RoleRequest $request)
    {
        $data = Role::query()
            ->select('id','zh_name','name')
            ->find($request->id);
        return $this->success($data);
    }

    public function update(Request $request)
    {
        $data = $data = Role::query()->find($request->id);
        $data->update($request->all());
        return $this->success('更新成功');
    }
    public function destory(Request $request)
    {
        $data = Role::query()->find($request->id);
        $data->delete();
        return $this->success('删除成功');
    }
}
